package apps.buscoplan;

import java.io.IOException;

import android.app.Activity;
import android.apps.events.R;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import apps.buscoplan.persist.Settings;

public class BuscoplanActivity extends Activity {
	public final String NONE_SELECTED_ACCOUNT = "none"; 
	public final static String SELECTED_ACCOUNT_NAME = "account_selected";
	public final String TAG = "BuscoplanActivity";
	
	protected String selectedAccount = NONE_SELECTED_ACCOUNT;
	protected ListView listAccounts;
	protected boolean profileExist = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
//    	setContentView(R.layout.main);
    	
    	//TODO Eliminar esto
    	try {
			Settings.saveAccount(NONE_SELECTED_ACCOUNT,this);
		} catch (IOException e1) {
			Log.e(TAG, e1.getMessage());
			e1.printStackTrace();
		}
    	
    	try {
			setSelectedAccount( Settings.getAccount(this) );
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		}
    	
    	setContentView(R.layout.main);
    	
    	checkSelectedAccountExists ();
    }
//    @Override
//    public void onResume() {
//    	super.onResume();
//    	Log.v(TAG, "Vuelta!");
//    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_CANCELED) {
        	Log.i(TAG, "No se ha seleccionado una cuenta, saliendo...");
            finish();
        }
        else {
        	if (data !=null)
        	{
	            setSelectedAccount(data.getStringExtra(SELECTED_ACCOUNT_NAME));
	        	checkSelectedAccountExists ();
	        	try {
					Settings.saveAccount(selectedAccount, this);
		        	Toast.makeText(getApplicationContext(),"Cuenta seleccionada: " + selectedAccount, Toast.LENGTH_LONG).show();

				} catch (IOException e) {
		        	Toast.makeText(getApplicationContext(),"Error al guardar los datos de " + selectedAccount, Toast.LENGTH_LONG).show();
					e.printStackTrace();
					finish();
				}
        	}
        }
    }
    
    private void checkSelectedAccountExists() {
    	
    	if (selectedAccount==null || selectedAccount.equals(NONE_SELECTED_ACCOUNT) ){
    		Toast.makeText(getApplicationContext(), "No hay cuenta seleccionada", Toast.LENGTH_SHORT);

//    		GoogleAccountManager googleAccountManager = new GoogleAccountManager( this );
//    		if (googleAccountManager.getAccountByName(selectedAccount) == null) {
	    		//lanzar selector de profiles
	        	Intent intentAccountList = new Intent(this, AccountsListView.class);
	        	intentAccountList.putExtra(SELECTED_ACCOUNT_NAME, NONE_SELECTED_ACCOUNT);
	        	startActivityForResult(intentAccountList, 0);
//    		}
//    		else {
//        		Toast.makeText(getApplicationContext(), selectedAccount, Toast.LENGTH_LONG);
//    		}
    	}
    	else {
    		Toast.makeText(getApplicationContext(), selectedAccount, Toast.LENGTH_LONG);
    	}
	}

	public void setSelectedAccount (String account){
    	if (selectedAccount == null){
    		selectedAccount = NONE_SELECTED_ACCOUNT;
    	}
    	else {
    		selectedAccount = account;
    	}
    }

}